/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.KhachHang_dao;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import pojo.ChiTietGioHang_pojo;
import pojo.KhachHang_pojo;
import captchas.CaptchasDotNet;

/**
 *
 * @author Rocker000
 */
public class KhachHang_controller extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try 
        {
            
            HttpSession session = request.getSession(); 
            String action = request.getParameter("action");
            CaptchasDotNet captchas = new captchas.CaptchasDotNet(request.getSession(true),"demo","secret");
            if(action.equals("dangnhap"))
            {                     
                String username = request.getParameter("txttendangnhap");
                String password = request.getParameter("txtmatkhau");
                if(KhachHang_dao.KiemTraDangNhap(username, password) ==0)
                {
                    String err = "Sai tên đăng nhập hoặc mật khẩu!";
                    request.setAttribute("err", err);
                    request.getRequestDispatcher("/Home.jsp").forward(request, response);
                            
                }
                else
                {
                     KhachHang_pojo kh=KhachHang_dao.DangNhap(username, password);
                     session.setAttribute("KhachHang",kh);
                     session.setAttribute("IsLogin", "True"); 
                     if(kh.KiemTraTonTaiChiTietGioHang()==1)
                         session.setAttribute("GioHang",kh.LayChiTietGioHang());
                     else
                     {
                         ArrayList<ChiTietGioHang_pojo> ds = new ArrayList<ChiTietGioHang_pojo>();
                         session.setAttribute("GioHang",ds);
                     }
                     request.setAttribute("err_login","Đăng Nhập Thành Công");
                     request.getRequestDispatcher("/Home.jsp").forward(request, response);
                }          
            }                        
            if(action.equals("dangxuat"))
            {
                session.invalidate();
                request.getRequestDispatcher("/Home.jsp").forward(request, response);               
            }            
            if(action.equals("dangki"))
            {
                String codeimage = (String)session.getAttribute("key");
                String code = request.getParameter("txtmabaove");
                if(codeimage.equals(code)==false)
                {
                    request.setAttribute("err","sai ma bao ve");
                    request.getRequestDispatcher("/DangKi.jsp").forward(request, response);
                }
                else
                {
                    if(KhachHang_dao.KiemTraTrungTen(request.getParameter("txtusername"))==1)
                    {
                        request.setAttribute("err","Tên đăng ki không hợp lệ! hãy chọn tên đăng kí khác!");
                        request.getRequestDispatcher("/DangKi.jsp").forward(request, response);
                    }
                    else
                    {
                        KhachHang_pojo x = new KhachHang_pojo();
                        x.setTenDangNhap(request.getParameter("txtusername"));
                        x.setMatKhau(request.getParameter("txtmatkhau"));
                        x.setEmail(request.getParameter("txtemail"));
                        //lấy chuổi unicode
                        String diachi=new String(request.getParameter("txtdiachi").getBytes("ISO8859_1"),"UTF8");
                        x.setDiaChi(diachi);
                        x.setSoDienThoai(request.getParameter("txtsodienthoai"));
                        String tenkhachhang=new String(request.getParameter("txttenkhachhang").getBytes("ISO8859_1"),"UTF8");
                        x.setTenKhachhang(tenkhachhang);
                        boolean kq = KhachHang_dao.ThemKhachHang(x); 
                        boolean taogiohang = x.TaoGioHang();         
                        if(kq==true&&taogiohang==true){    
                            request.setAttribute("err","Đăng Ký Thành Công");
                            request.getRequestDispatcher("/Home.jsp").forward(request, response);}
                    }
                }                                                                                
            }
            
            if(action.equals("capnhatthongtin"))
            {
                String tenthat= new String(request.getParameter("txttenthat").getBytes("ISO8859_1"),"UTF8");
                String diachi= new String(request.getParameter("txtdiachi").getBytes("ISO8859_1"),"UTF8");
                String email= new String(request.getParameter("txtemail").getBytes("ISO8859_1"),"UTF8");
                String sodienthoai= new String(request.getParameter("txtsodienthoai").getBytes("ISO8859_1"),"UTF8");
                KhachHang_pojo kh = (KhachHang_pojo)session.getAttribute("KhachHang");
                KhachHang_dao.CapNhatThongTin(tenthat, email, diachi, sodienthoai,kh.getTenDangNhap());
                
                
                kh.setDiaChi(diachi);
                kh.setEmail(email);
                kh.setTenKhachhang(tenthat);
                kh.setSoDienThoai(sodienthoai);
                session.setAttribute("KhachHang", kh);
                request.getRequestDispatcher("/thongtincanhan.jsp").forward(request, response);
            }
            
            if(action.equals("thaydoimatkhau"))
            {
                String matkhau= new String(request.getParameter("txtmatkhau").getBytes("ISO8859_1"),"UTF8");
                String matkhaumoi= new String(request.getParameter("txtmatkhaumoi").getBytes("ISO8859_1"),"UTF8");
                String nhaclaimatkhaumoi= new String(request.getParameter("txtnhaclaimatkhaumoi").getBytes("ISO8859_1"),"UTF8");
            }
        } 
        finally 
        {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
